package wxdb.parse;
import wxdb.errmsg.ErrorMsg;
import java.io.*;
import javax.swing.*;

%% 

%public
%implements Lexer
%function nextToken
%type Token
%line
%char
%state STRING

ALPHA=[A-Za-z]
DIGIT=[0-9]
WHITE_SPACE_CHAR=[\n\ \t\b\012]

%{
private Token tok(int position,int kind, Object value) {
    	return new Token(position,kind, yychar, yychar+yylength(),value);
}

public Yylex(java.io.InputStream s, ErrorMsg e) {
  this(s);
  errorMsg=e;
}

public Yylex (java.io.File file,String filename,ErrorMsg e){
	this();
	if (null == file) {
		throw (new Error("Error: Bad file initializer."));
	}
	try{
		yy_reader = new java.io.BufferedReader(new FileReader(file));
		errorMsg = e;
	}catch(Exception err){};
}

public Yylex(String cmd){
	this();
	try{
		String fileName = "tmpcmd";
 		RandomAccessFile randomFile = new RandomAccessFile(fileName, "rw");
		randomFile.writeBytes(cmd);
		randomFile.close();
		File file = new File(fileName);
		yy_reader = new java.io.BufferedReader(new FileReader(file));
		errorMsg = new ErrorMsg("tmpin");
	}catch(Exception e){};
}

private ErrorMsg errorMsg;
private String string_content = "";
%}

%eofval{
	{
		return tok(yyline,sym.EOF, null);
    }
%eofval}       

%%

<YYINITIAL> "'"				{yybegin(STRING);string_content = "";}
<STRING> "'"	{       
				yybegin(YYINITIAL);                
				return tok(yyline,sym.STRING,string_content);
		}
<STRING> . 		{string_content+=yytext();}



<YYINITIAL> " "		{}
<YYINITIAL> "SELECT" 	{return tok(yyline,sym.SELECT, null);}
<YYINITIAL> "FROM" 	{return tok(yyline,sym.FROM, null);}
<YYINITIAL> "WHERE" {return tok(yyline,sym.WHERE,null);}
<YYINITIAL> "GROUP" 	{return tok(yyline,sym.GROUP,null);}
<YYINITIAL> "BY" 	{return tok(yyline,sym.BY,null);}
<YYINITIAL> "HAVING" 	{return tok(yyline,sym.HAVING,null);}
<YYINITIAL> "ORDER" 	{return tok(yyline,sym.ORDER,null);}
<YYINITIAL> "AVG" 	{return tok(yyline,sym.AVG,null);}
<YYINITIAL> "COUNT" 	{return tok(yyline,sym.COUNT,null);}
<YYINITIAL> "MIN" 	{return tok(yyline,sym.MIN,null);}
<YYINITIAL> "MAX" 	{return tok(yyline,sym.MAX,null);}
<YYINITIAL> "SUM" 	{return tok(yyline,sym.SUM,null);}
<YYINITIAL> "AS" 	{return tok(yyline,sym.AS,null);}
<YYINITIAL> "+" 	{return tok(yyline,sym.PLUS,null);}
<YYINITIAL> "-" 	{return tok(yyline,sym.MINUS,null);}
<YYINITIAL> "*" 	{return tok(yyline,sym.TIMES,null);}
<YYINITIAL> "/" 	{return tok(yyline,sym.DIVIDE,null);}
<YYINITIAL> "AND" 	{return tok(yyline,sym.AND,null);}
<YYINITIAL> "OR" 	{return tok(yyline,sym.OR,null);}
<YYINITIAL> "<" 	{return tok(yyline,sym.LT,null);}
<YYINITIAL> "<=" 	{return tok(yyline,sym.LE,null);}
<YYINITIAL> ">" 	{return tok(yyline,sym.GT,null);}
<YYINITIAL> ">=" 	{return tok(yyline,sym.GE,null);}
<YYINITIAL> "=" 	{return tok(yyline,sym.EQ,null);}
<YYINITIAL> "<>" 	{return tok(yyline,sym.NEQ,null);}
<YYINITIAL> "IN" 	{return tok(yyline,sym.IN,null);}
<YYINITIAL> "CREATE" 	{return tok(yyline,sym.CREATE,null);}
<YYINITIAL> "TABLE" 	{return tok(yyline,sym.TABLE,null);}
<YYINITIAL> "NOT" 	{return tok(yyline,sym.NOT,null);}
<YYINITIAL> "NULL" 	{return tok(yyline,sym.NULL,null);}
<YYINITIAL> "DEFAULT" 	{return tok(yyline,sym.DEFAULT,null);}
<YYINITIAL> "CHAR" 	{return tok(yyline,sym.CHAR,null);}
<YYINITIAL> "VARCHAR" 	{return tok(yyline,sym.VARCHAR,null);}
<YYINITIAL> "DATE" 	{return tok(yyline,sym.DATE,null);}
<YYINITIAL> "TIME" 	{return tok(yyline,sym.TIME,null);}
<YYINITIAL> "REAL" 	{return tok(yyline,sym.REAL,null);}
<YYINITIAL> "DOUBLE" 	{return tok(yyline,sym.DOUBLE,null);}
<YYINITIAL> "FLOAT" 	{return tok(yyline,sym.FLOAT,null);}
<YYINITIAL> "INTEGER" 	{return tok(yyline,sym.INTEGER,null);}
<YYINITIAL> "LONG" 	{return tok(yyline,sym.LONG,null);}
<YYINITIAL> "DECIMAL" 	{return tok(yyline,sym.DECIMAL,null);}
<YYINITIAL> "INSERT" 	{return tok(yyline,sym.INSERT,null);}
<YYINITIAL> "INTO" 	{return tok(yyline,sym.INTO,null);}
<YYINITIAL> "VALUES" 	{return tok(yyline,sym.VALUES,null);}
<YYINITIAL> "DELETE" 	{return tok(yyline,sym.DELETE,null);}
<YYINITIAL> "UPDATE" 	{return tok(yyline,sym.UPDATE,null);}
<YYINITIAL> "SET" 	{return tok(yyline,sym.SET,null);}
<YYINITIAL> "PRIMARY" 	{return tok(yyline,sym.PRIMARY,null);}
<YYINITIAL> "KEY" 	{return tok(yyline,sym.KEY,null);}
<YYINITIAL> "FOREIGN" 	{return tok(yyline,sym.FOREIGN,null);}
<YYINITIAL> "FLT" {return tok(yyline,sym.FLT,null);}
<YYINITIAL> ((-)?){DIGIT}+ 				{return tok(yyline,sym.INT,new Integer(yytext()));}
<YYINITIAL> {ALPHA}({ALPHA}|{DIGIT}|_)* 	{return tok(yyline,sym.ID,yytext());}
<YYINITIAL> "," 	{return tok(yyline,sym.COMM,null);}
<YYINITIAL> ";" 	{}
<YYINITIAL> "(" 	{return tok(yyline,sym.LPAREN,null);}
<YYINITIAL> ")" 	{return tok(yyline,sym.RPAREN,null);}
<YYINITIAL> "." 	{return tok(yyline,sym.PERIOD,null);}
<YYINITIAL> {WHITE_SPACE_CHAR}+ 	{ }
<YYINITIAL> . 		{return tok(yyline,sym.error,"Error: Unmatched Input!");}